package com.caltco.cargo.ops.common.base.gen;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.caltco.cargo.ops.common.api.CommonPage;
import com.caltco.cargo.ops.common.api.R;
import com.caltco.cargo.ops.common.api.R2;
import com.caltco.cargo.ops.common.api.Result;
import com.caltco.cargo.ops.common.base.BaseEntity;
import io.swagger.v3.oas.annotations.Operation;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.Serializable;
import java.util.List;
import java.util.Map;

/**
 * 通用请求处理
 *
 * @author Yaolong
 * @since v1.0.0
 */
@SuppressWarnings("all")
public abstract class BaseController<T extends BaseEntity, S extends IBaseService<T>> {

	@Resource
	protected HttpServletRequest request;

	@Autowired
	protected S service;

	/**
	 * 新增
	 * @param domain 领域模型
	 * @return {@link R}
	 */
	@PostMapping("")
	@Operation(summary = "新增")
	public Result create(@Valid @RequestBody T domain) {
		// 业务逻辑
		boolean created = service.create(domain);
		if (created) {
			return R2.ok("创建成功");
		}
		return R2.err("创建失败");
	}

	/**
	 * 删除
	 * @param id {@code Long}
	 * @return {@link R}
	 */
	@DeleteMapping("{id}")
	@Operation(summary  = "删除")
	public Result remove(@PathVariable Serializable id) {
		// 业务逻辑
		boolean deleted = service.remove(id);
		if (deleted) {
			return R2.ok("删除成功");
		}

		return R2.err("删除失败");
	}

	/**
	 * 修改
	 * @param domain 领域模型
	 * @return {@link R}
	 */
	@PutMapping("")
	@Operation(summary  = "修改")
	public Result update(@Valid @RequestBody T domain) {
		System.out.println(domain);
		// 业务逻辑
		boolean updated = service.update(domain);
		if (updated) {
			return R2.ok("编辑成功");
		}

		return R2.err("编辑失败");
	}

	/**
	 * 获取
	 * @param id {@code Long}
	 * @return {@link R}
	 */
	@GetMapping("{id}")
	@Operation(summary  = "根据id获取")
	public Result<?> get(@PathVariable Serializable id) {
		T domain = service.get(id);
		return R2.ok(domain);
	}

	/**
	 * 分页
	 * @param current {@code int} 页码
	 * @param size {@code int} 笔数
	 * @return {@link R}
	 */
	@GetMapping("page")
	@Operation(summary  = "分页查询")
	public Result<CommonPage<List<?>>> page(@RequestParam int page, @RequestParam int pageSize, @ModelAttribute T domain) {
		IPage<?> pageInfo = service.page(page, pageSize, domain);
		CommonPage<List<?>> objectCommonPage = new CommonPage<List<?>>().setData(pageInfo.getRecords(), pageInfo.getTotal());
		return R2.ok(objectCommonPage);
	}


	/**
	 * 获取列表
	 * @param domain
	 * @return {@link R}
	 */
	@GetMapping("list")
	@Operation(summary  = "获取列表")
	public Result<List<?>> list( @ModelAttribute T domain) {
		List<?> list = service.list(domain);
		return R2.ok(list);
	}

	/**
	 * 删除
	 * @param id {@code Long}
	 * @return {@link R}
	 */
	@PostMapping("batch")
	@Operation(summary  = "删除")
	public Result remove(@RequestBody Map<String,List<Serializable>> map) {
		// 业务逻辑
		boolean deleted = service.removeBatch(map.get("ids"));
		if (deleted) {
			return R2.ok("删除成功");
		}
		return R2.err("删除失败");
	}

	/**
	 * 导出
	 * @param id {@code Long}
	 * @return {@link R}
	 */
	@PostMapping("export")
	@Operation(summary = "导出")
	public Result exportFile(List<Serializable> ids) {
		// 业务逻辑
		boolean exported = service.exportFile(ids);
		if (exported) {
			return R2.ok("导出成功");
		}
		return R2.err("导出失败");
	}

	/**
	 * 导出
	 * @param id {@code Long}
	 * @return {@link R}
	 */
	@PostMapping("exportTemp")
	@Operation(summary = "导出")
	public void exportTempFile(HttpServletResponse response, T domain) {
		// 业务逻辑
		boolean exported = service.exportTempFile(response,domain);
	}

	/**
	 * 导入模版
	 * @param id {@code Long}
	 * @return {@link R}
	 */
	@PostMapping("import")
	@Operation(summary  = "导入")
	public Result importFile(MultipartFile file,@ModelAttribute T domain) {
		// 业务逻辑
		boolean exported = service.importFile(file,domain);
		if (exported) {
			return R2.ok("导入成功");
		}
		return R2.err("导入失败");
	}

	/**
	 * 获取选择狂列表
	 * @param id {@code Long}
	 * @return {@link R}
	 */
	@GetMapping("/getSearch")
	@Operation(summary = "获取选择狂列表")
	public Result<List<?>> getSearch(@RequestParam(value = "search",required = false) String search) {
		List<?> list = service.getSearch(search);
		return R2.ok(list);
	}

	/**
	 * 批量新增修改
	 * @param domain 领域模型
	 * @return {@link R}
	 */
	@PostMapping("/saveUploadBatch")
	@Operation(summary = "批量新增修改")
	public Result saveUploadBatch(@Valid @RequestBody List<T> domain) {
		// 业务逻辑
		boolean created = service.saveUploadBatch(domain);
		if (created) {
			return R2.ok("创建成功");
		}
		return R2.err("创建失败");
	}
}
